package ai.george.rag.controller;


import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "ChatClient示例")
@RestController
@RequestMapping("/chat-client")
public class ChatClientController {


    private static final String DEFAULT_PROMPT = "你是一个博学的智能聊天助手，请根据用户提问回答！";

    private final ChatClient dashScopeChatClient;


    /**
     * 使用构造器注入ChatClient
     * @param chatClientBuilder 由starter自动创建
     */
    public ChatClientController(ChatClient.Builder chatClientBuilder) {
        DashScopeChatOptions chatOptions = DashScopeChatOptions.builder()
                .withModel(DashScopeApi.ChatModel.QWEN_PLUS.getValue())
                .withTopP(0.7)
                .build();

        this.dashScopeChatClient = chatClientBuilder
//                .defaultSystem(DEFAULT_PROMPT)
                // 实现 Logger 的 Advisor
                .defaultAdvisors(
//                        new SimpleLoggerAdvisor()
                )
                // 设置 ChatClient 中 ChatModel 的 Options 参数
                .defaultOptions(chatOptions)
                .build();
    }

    @Operation(summary = "ChatClient-1-返回String文本", description = "使用的是ChatClient.call().content()调用方式")
    @GetMapping("/text")
    public String simple(@RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") @Parameter(name = "query", description = "请求内容") String query) {

        String content = dashScopeChatClient
                .prompt()
                .system(DEFAULT_PROMPT)
                .user(query)
                .call()
                .content();
        return content;
    }


    @Operation(summary = "ChatClient-2-返回ChatResponse", description = "使用的是ChatClient.call().chatResponse()调用方式")
    @GetMapping("/chat-response")
    public ChatResponse chatResponse(@RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") @Parameter(name = "query", description = "请求内容") String query) {
        ChatResponse chatResponse = dashScopeChatClient
                .prompt()
                .system(DEFAULT_PROMPT)
                .user(query)
                .call()
                .chatResponse();
        return chatResponse;
    }






}
